package com.ele.client.activity.home;

import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.*;
import com.github.gwtbootstrap.client.ui.TextBox;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.*;

public class HomeViewImpl extends Composite implements HomeView {

    interface DefaultUiBinder extends UiBinder<Widget, HomeViewImpl> {}
    private static DefaultUiBinder uiBinder = GWT.create(DefaultUiBinder.class);

    private Presenter presenter;

    @UiField
    PageHeader pageHeader;
    @UiField
    FlowPanel wordsPanel;
    @UiField
    ControlLabel questionLabel;
    @UiField
    TextBox answerTextBox;
    @UiField
    Button button;
    @UiField
    ControlGroup controlGroup;
    @UiField
    HelpBlock helpBlock;
    @UiField
    Image image;
    @UiField
    HTML descriptionPlaceholder;

    public HomeViewImpl() {
        initWidget(uiBinder.createAndBindUi(this));

        image.setVisible(false);
    }

    @Override
    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void startLoading() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void stopLoading() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void setDescriptionPlaceholder(String description) {
        descriptionPlaceholder.setHTML(description);
    }

    @Override
    public void setQuestion(String text) {
        questionLabel.getElement().setInnerHTML(text);
    }

    @Override
    public void setName(String name) {
        pageHeader.setText(name);
    }

    @Override
    public void addWord(IsWidget widget) {
        wordsPanel.add(widget);
    }

    @Override
    public void showHelp(String html) {
        helpBlock.getElement().setInnerHTML(html);
    }

    @Override
    public void clearWords() {
        wordsPanel.clear();
    }

    @Override
    public void clearHelp() {
        helpBlock.setText("");
    }

    @UiHandler("button")
    public void onButtonClick(ClickEvent event) {
        presenter.checkAnswer(answerTextBox.getValue());
    }

    @UiHandler("answerTextBox")
    public void onKeyDown(KeyDownEvent event) {
        if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
            presenter.checkAnswer(answerTextBox.getValue());
        }

//        if (answerTextBox.getValue().isEmpty()) {
//            button.setText("Не знаю (Enter)");
//        } else {
//            button.setText("Проверить (Enter)");
//        }
    }
}
